<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\AddressRecorder;
use app\models\KuaidisendRecorder;
use app\models\MemberRecorder;
use app\models\OrderRecorder;

class OrderController extends BaseController{

    public function actionList(){
        $post = \Yii::$app->request->post('pagination');
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $pay = intval($post['pay']);
        $status = intval($post['status']);
        $send = intval($post['send']);
        $offset = $page*$pagesize;
        $list = OrderRecorder::find();
        switch ($pay){
            case 0:
                break;
            case 1:
                $list = $list->andWhere('is_pay=1');
                break;
            case 2:
                $list = $list->andWhere('is_pay=0');
                break;
        }
        switch ($status){
            case 0:
                break;
            case 1:
                $list = $list->andWhere('status=0');
                break;
            case 2:
                $list = $list->andWhere('status=1');
                break;
        }
        switch($send){
            case 0:
                break;
            case 1:
                $list = $list->andWhere("is_send=0");
                break;
            case 2:
                $list = $list->andWhere('is_send=1');
                break;
        }
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['order_content'] = json_decode($v['order_content'],true);
            $list[$k]['goods_attr'] = json_decode($v['goods_attr'],true);
            $list[$k]['address'] = empty($v['address'])?[]:json_decode($v['address'],true);
            $list[$k]['hd_price'] = json_decode($v['hd_price'],true);
            $list[$k]['address'] = json_decode($v['address'],true);
            $list[$k]['order_else'] = json_decode($v['order_else'],true);
            $user = MemberRecorder::find()->where('id='.$v['user_id'])->one();
            $arr['user_name'] = empty($user->user_name)?\Yii::$app->params['non_name']:$user->user_name;
            $arr['user_pic'] = empty($user->user_pic)?\Yii::$app->params['base_url'].\Yii::$app->params['non_pic']:\Yii::$app->params['base_url'].$user->user_pic;
            $list[$k]['user'] = $arr;
            $k_model = KuaidisendRecorder::find()->where('id='.$v['k_name'])->one();
            $list[$k]['k_name'] = empty($k_model)?"未知":$k_model->k_name;
        }
        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionSend(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->is_send=1;
        $model->save(false);
        //发送订阅通知
        $data = $this->get_message(1);
        foreach($data['data'] as $k=>$v){
            $arr2['value'] = $v['value'];

            if($v['value']=="order_time"){
                $arr2['value'] = date('Y年m月d日 H:i:s',$model->addtime);
            }

            if($v['value']=="order_send_time"){
                $arr2['value'] = date('Y年m月d日 H:i:s',time());
            }

            if($v['value']=="order_end_time"){
                $s_time = \Yii::$app->params['send_time']*60;
                $arr2['value'] = date('Y年m月d日 H:i:s',strtotime("+ ".$s_time." minute",time()));
            }

            if($v['value']=="order_no"){
                $arr2['value'] = $model->order_name;
            }

            if($v['value']=="order_price"){
                $arr2['value'] = $model->total_price;
            }

            if($v['value']=="order_goods"){
                $goods = json_decode($model->order_content,true);
                $g=[];
                foreach($goods as $g_v){
                    foreach($g_v['list'] as $g_v2){
                        $g[] = $g_v2['goods_name']."-".implode("|",$g_v2['attr'])."-数量".$g_v2['goods_num'];
                    }
                }
                $arr2['value'] = implode(",",$g);
            }
            $arr[$v['key']]=$arr2;

        }
        $data['data']=$arr;
        $this->send_message($data,$model->user_id);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionPrice(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->select(['id','order_name','user_id','total_price'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }

        $user = MemberRecorder::find()->where('id='.$model['user_id'])->one();
        $model['user_name'] = $user->user_name;
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSaveprice(){
        $post = \Yii::$app->request->post('form');
        $id=  $post['id'];
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        if($model->is_pay){
            $result = ['result'=>0,'data'=>'订单已支付，不能修改价格'];
            return json_encode($result);
        }
        $model->total_price = abs(doubleval($post['price']));
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionEnd(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->is_send=1;
        $model->is_get=1;
        $model->is_end=1;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionStatus(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->status=!$model->status;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionSavesend(){
        $post = \Yii::$app->request->post('form');
        $id = intval($post['id']);
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        if($model->is_end||$model->is_get){
            $result = ['result'=>0,'data'=>'订单已完成或已收货，不能修改物流'];
            return json_encode($result);
        }
        $model->k_name = $post['k_name'];
        $model->k_order = $post['k_order'];
        $model->is_send=1;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionKuaidi(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->select(['id','k_order','k_name'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(empty($model)){
            $result = ['result'=>0,'data'=>'不存在相关订单，请重新操作'];
            return json_encode($result);
        }
        $k_model = KuaidisendRecorder::find()->where('id='.$model['k_name'])->one();
        $model['k_name'] = empty($k_model)?"未知":$k_model->k_name;
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

}
